<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ArchiveController extends Controller
{
    // 分类页主页
    public function index()
    {
        // SELECT YEAR(a.created_at) year,MONTH(a.created_at) month,COUNT(*) count FROM `articles` as a GROUP BY year,month
        $archives = DB::select('SELECT YEAR(a.created_at) year,MONTH(a.created_at) month,COUNT(*) count FROM `articles` as a GROUP BY year,month');
        return view('archive', ['archives' => $archives, 'articles' => []]);
    }


    // 某个分类下的所有文章
    public function detail(Request $request, $year, $month)
    {
        $archives = DB::select('SELECT YEAR(a.created_at) year,MONTH(a.created_at) month,COUNT(*) count FROM `articles` as a GROUP BY year,month');

        // SELECT a.id,a.title,a.description FROM `articles` a WHERE YEAR(a.created_at) = '2021' AND MONTH(a.created_at) = '8'
        $articles = DB::select('SELECT a.id,a.title,a.description,a.created_at FROM `articles` a WHERE YEAR(a.created_at) = ? AND MONTH(a.created_at) = ?', [$year, $month]);
        return view('archive', ['archives' => $archives, 'articles' => $articles]);
    }
}
